//
//  UIFont+Ex.swift
//  
//
//  Created by tank on 2021/12/16.
//

import Foundation
import AppFoundation
import UIKit

extension Extension where Base == UIFont {

    public static func iconFont(ofSize size: CGFloat) -> UIFont {
        return IconFonts.shared.iconFont(size: size)
    }

    public static func registerFont(forResource: String, identifier: String) -> Bool {

        guard let bundle = Bundle(identifier: identifier) else {
            return false
        }

        return registerFont(forResource: forResource, bundle: bundle)
    }

    public static func registerFont(forResource: String, bundle: Bundle) -> Bool {

        guard let path = bundle.path(forResource: forResource, ofType: nil) else {
            return false
        }

        guard let fontData = NSData(contentsOfFile: path) else {
            return false
        }

        guard let dataProvider = CGDataProvider(data: fontData) else {
            return false
        }

        var errorRef: Unmanaged<CFError>?
        if !CTFontManagerRegisterGraphicsFont(CGFont(dataProvider)!, &errorRef) {
            return false
        }

        return true
    }
}
